home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / msgraph.lzh / GRAPHDMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-01  |  5.3 KB  |  253 lines

  1. /* graphdmo.c - demonstration of the graphics library mgraph.c/msgraph.asm
  2.  * This program uses functions setvid(), fcircle() in file mgraph.c,
  3.  * pset(), line(), fline() in file msgraph.asm, and ci() in file ci.asm.
  4.  *
  5.  * by Paul Ketrick
  6.  */
  7.  
  8. #include <math.h>            /* contains declarations for math functions */
  9.  
  10. #define PI 3.14159265359
  11. #define sign(x) (x==0 ? 0 : (x < 0 ? -1 : 1))
  12.  
  13. extern double aspect;        /* video display aspect ratio - contained in
  14.                              * mgraph.c
  15.                              */
  16.  
  17. main()
  18. {
  19.     char c;
  20.  
  21.     for (;;)  {
  22.         setvid(2);            /* initialize 80x25 text mode */ 
  23.         printf("Graphics demonstration program.\n\n");
  24.         printf("A - Polygon drawing (640x200 mode)\n");
  25.         printf("B - Polygon drawing (320x200 mode)\n");
  26.         printf("C - Line-drawing\n");
  27.         printf("D - 160x100 Rainbow\n");
  28.         printf("E - 160x100 Color spectrum\n");
  29.         printf("F - Circle-drawing\n");
  30.         printf("X - Exit\n");
  31.         printf("Please enter your selection:");
  32.  
  33.         for (c=0; c== 0;)  {
  34.             while ((c=ci()) == -1)        /* wait for a key to be pressed */
  35.                 ;
  36.             switch(tolower(c))  {        /* call the selected routine */
  37.                 case 'a':
  38.                     polygon6();
  39.                     break;
  40.  
  41.                 case 'b':
  42.                     polygon3();
  43.                     break;
  44.  
  45.                 case 'c':
  46.                     lines();
  47.                     break;
  48.  
  49.                 case 'd':
  50.                     rainbow();
  51.                     break;
  52.  
  53.                 case 'e':
  54.                     spectrum();
  55.                     break;
  56.  
  57.                 case 'f':
  58.                     circles();
  59.                     break;
  60.  
  61.                 case 'x':
  62.                     exit(0);
  63.  
  64.                 default:
  65.                     c=0;
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. polygon6()
  72. {
  73.     int sides, i, j;
  74.     int x[50], y[50];
  75.     double deg;
  76.  
  77.     printf("\nEnter the number of sides to use:");
  78.     scanf("%d", &sides);
  79.  
  80.     setvid(6);                /* initialize 640x200 mode */
  81.  
  82.     i=0;
  83.     /* build array containing coordinates for each point on the polygon's
  84.      * perimeter:
  85.      */
  86.     for (deg=0; deg < 2*PI; deg+=2*PI / sides)  {
  87.         x[i]=320 + aspect * 99 * cos(deg);
  88.         y[i]=100 + 99 * sin(deg);
  89.         ++i;
  90.     }
  91.  
  92.     /* draw a line from every point to every other point: */
  93.     for (i=0; i < sides-1; i++)
  94.         for (j=i+1; j < sides; j++)
  95.             fline(x[i], y[i], x[j], y[j], 1);
  96.  
  97.     while (ci() == -1)
  98.         ;
  99. }
  100.  
  101. polygon3()
  102. {
  103.     int sides, i, j;
  104.     double deg, aspect;
  105.     int x[50], y[50];
  106.  
  107.     printf("\nEnter the number of sides to use:");
  108.     scanf("%d", &sides);
  109.  
  110.     aspect=1.0;        /* aspect ratio for my monitor */
  111.     i=0;
  112.     for (deg=0; deg < 2*PI; deg+=2*PI / sides)  {
  113.         x[i]=160 + aspect * 99 * cos(deg);
  114.         y[i]=100 + 99 * sin(deg);
  115.         ++i;
  116.     }
  117.  
  118.     setvid(4);        /* initialize 320x200 graphics mode */
  119.  
  120.     for (i=0; i < sides-1; i++)
  121.         for (j=i+1; j < sides; j++)
  122.             fline(x[i], y[i], x[j], y[j], 1);
  123.  
  124.     while (ci() == -1)
  125.         ;
  126. }
  127.  
  128. double rnd();    /* source code for this function is below */
  129.  
  130. #define NUMLINES 150
  131.  
  132. lines()        /* this one works really well only on a system equipped
  133.              * with a math coprocessor - this is due to the slow
  134.              * random number generator
  135.              */
  136. {
  137.     static int x1[NUMLINES], y1[NUMLINES], x2[NUMLINES], y2[NUMLINES];
  138.     int in=1, out=2, i, j, x=0, y=0, x0=0, y0=0, dx=2, dy=3, dx0=1, dy0=-1;
  139.  
  140.     while (x < 2 || x > 637 || y < 2 || y > 197)  {
  141.         printf("\nEnter the beginning coordinates for x1 and y1:");
  142.         scanf("%d,%d", &x, &y);
  143.     }
  144.  
  145.     while (x0 < 2 || x0 > 637 || y0 < 2 || y0 > 197)  {
  146.         printf("Enter the beginning coordinates for x2 and y2:");
  147.         scanf("%d,%d", &x0, &y0);
  148.     }
  149.  
  150.     setvid(6);                        /* initialize 640x200 graphics mode */
  151.  
  152.     for (i=0; i<NUMLINES; ++i)
  153.         x1[i]=y1[i]=x2[i]=y2[i]=0;
  154.  
  155.     while (ci() == -1)  {
  156.         while ((x+=dx) < 0 || x > 639)  {
  157.             dx=(x < 320 ? 1 : -1) * rnd() * 5;
  158.         }
  159.  
  160.         while ((y+=dy) < 0 || y > 199)  {
  161.             dy=(y < 100 ? 1 : -1) * rnd() * 3;
  162.         }
  163.  
  164.         while ((x0+=dx0) < 0 || x0 > 639)  {
  165.             dx0=(x0 < 320 ? 1 : -1) * rnd() * 5;
  166.         }
  167.  
  168.         while ((y0+=dy0) < 0 || y0 > 199)  {
  169.             dy0=(y0 < 100 ? 1 : -1) * rnd() * 3;
  170.         }
  171.  
  172.         x1[in]=x;
  173.         y1[in]=y;
  174.         x2[in]=x0;
  175.         y2[in]=y0;
  176.         fline(x, y, x0, y0, 1);
  177.         fline(x1[out], y1[out], x2[out], y2[out], 0);
  178.  
  179.         ++out;
  180.         if (out > NUMLINES)
  181.             out=0;
  182.  
  183.         ++in;
  184.         if (in > NUMLINES)
  185.             in=0;
  186.     }
  187. }
  188.  
  189. rainbow()        /* also only really good on 8087 systems */
  190. {
  191.     int i, color;
  192.     float deg, inc;
  193.  
  194.     setvid(8);
  195.  
  196.     for (i=25; i < 79; ++i)  {
  197.         inc=PI/4 / i;
  198.         for (deg=0; deg < PI; deg+=inc)
  199.             pset(80 + (int)(i * cos(deg)), 82 - (int)(i * sin(deg)), color);
  200.         color = (color==15) ? 1 : ((color==7) ? 9 : color+1);
  201.     }
  202.  
  203.     while (ci() == -1)
  204.         ;
  205. }
  206.  
  207. double rnd()    /* fairly good (?) pseudo-random number generator
  208.                  * returns a double-precision number between 0 and 1
  209.                  */
  210. {
  211.     static double seed;
  212.     double num;
  213.  
  214.     seed=cos(seed * seed) / log(seed + 2);
  215.     num=seed * 1000;
  216.     return(num - floor(num));
  217. }
  218.  
  219. spectrum()        /* display the 160x100 graphics mode's color spectrum */
  220. {
  221.     int i, j;
  222.  
  223.     setvid(8);                    /* initialize 160x100 b/w mode */
  224.  
  225.     for (j=0; j < 40; ++j)
  226.         for (i=0; i < 160; ++i)
  227.             pset(i, j, i+j);
  228.  
  229.     for (i=0; i < 16; ++i)
  230.         fillbox(i*10, 50, i*10+8, 99, i);
  231.  
  232.     while (ci() == -1)
  233.         ;
  234. }
  235.  
  236. circles()                        /* draws neat circle patterns */
  237. {
  238.     double angle;
  239.     int c;
  240.  
  241.     setvid(6);                    /* initialize 640x200 mode */
  242.     for (c=0; c<10; ++c)  {
  243.         for (angle=0.0; angle<6.0*PI; angle+=PI/20.0)
  244.             fcircle((int)(148.0*cos(angle)+320.0), (int)(74.0*sin(angle)+100.0),
  245.                 (int)(angle*4.0/PI), 1);
  246.  
  247.         for (angle=0.0; angle<6.0*PI; angle+=PI/20.0)
  248.             fcircle((int)(148.0*cos(angle)+320.0), (int)(74.0*sin(angle)+100.0),
  249.                 (int)(angle*4.0/PI), 0);
  250.     }
  251.     setvid(2);
  252. }
  253.